﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class AddNewStudent : Form
    {
        public AddNewStudent()
        {
            InitializeComponent();
        }

        private void AddNewStudent_Load(object sender, EventArgs e)
        {
            //帐号框禁用
            this.txt_UserName.Enabled = false;
            Random AcRand = new Random();
            int Account = AcRand.Next(1723000000, 1723009999);
            this.txt_UserName.Text = Account.ToString();
            //密码框禁用
            this.txt_PassWord.Enabled = false;
            int PassWord = 666666;
            this.txt_PassWord.Text = PassWord.ToString();
            //绑定年
            ArrayList alyear = new ArrayList();
            int i;
            for (i = 1900; i <= 2099; i++)
                alyear.Add(i);
            this.cbo_Years.DataSource = alyear;
            //绑定月
            ArrayList almonth = new ArrayList();
            for (i = 1; i <= 12; i++)
                almonth.Add(i);
            this.cbo_Months.DataSource = almonth;
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = Int32.Parse(this.cbo_Months.SelectedValue.ToString());
            binddays(year, month);//绑定天 
        }

        //判断闰年 
        private bool checkleap(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                return true;
            else return false;
        }

        //绑定每月的天数 
        private void binddays(int year, int month)
        {
            int i;
            ArrayList alday = new ArrayList();
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    for (i = 1; i <= 31; i++)
                        alday.Add(i);
                    break;
                case 2:
                    if (checkleap(year))
                    {
                        for (i = 1; i <= 29; i++)
                            alday.Add(i);
                    }
                    else
                    {
                        for (i = 1; i <= 28; i++)
                            alday.Add(i);
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    for (i = 1; i <= 30; i++)
                        alday.Add(i);
                    break;
            }
            this.cbo_Days.DataSource = alday;
        }

        private void cbo_Years_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = 1;
            binddays(year, month);
        }

        private void cbo_Months_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = Int32.Parse(this.cbo_Months.SelectedValue.ToString());
            binddays(year, month);
        }

        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            string UserName = txt_UserName.Text;
            string PassWord = txt_PassWord.Text;
            string RealName = txt_RealName.Text;
            string IdentityCard = txt_IdentityCard.Text;
            string Sex = "女";
            if (rdo_Man.Checked == true)
            {
                Sex = "男";
            }
            if (rdo_Woman.Checked)
            {
                Sex = "女";
            }
            string GradeName = cbo_GradeName.Text;
            int GradeId=0;
            if (GradeName == "大一")
            {
                GradeId = 1;
            }
            if (GradeName == "大二")
            {
                GradeId = 2;
            }
            if (GradeName == "大三")
            {
                GradeId = 3;
            }
            var dte = Convert.ToDateTime(string.Format("{0}-{1}-{2}", cbo_Years.Text.Trim(),
                                                                      cbo_Months.Text.Trim(),
                                                                      cbo_Days.Text.Trim()));
            string sql = string.Format(@"insert into dbo.UserInformation(UserName,PassWord,RealName,IdentityCard,Sex,GradeId,Birthday)
                                         values({0},'{1}','{2}','{3}','{4}','{5}','{6}')", UserName, PassWord, RealName, IdentityCard, Sex, GradeId, dte);
            int number = Convert.ToInt32(db.ExecNonQuery(sql));
            if (number > 0)
            {
                MessageBox.Show("插入成功");
            }
            else
            {
                MessageBox.Show("插入失败");
            }
        }
    }
}
